#ifndef USRAI_H
#define USRAI_H

#include "AI.h"
#include "config.h"
#include "GlobalVariate.h"
#include "Coordinate.h"
#include <vector>
#include <map>
#include <utility>
#include <ctime>
#include <cstdlib>

// 声明全局变量
extern tagGame tagUsrGame;
extern ins UsrIns;

class UsrAI : public AI
{
public:
    UsrAI() { 
        this->id = 0; 
        srand(static_cast<unsigned int>(time(nullptr)));
    }
    ~UsrAI() {}

private:
    void processData() override;
    
    // 获取游戏信息
    tagInfo getInfo();
    
    // 房屋建造相关函数
    void checkHouseStatus(const tagInfo& myInfo);
    std::pair<int, int> findSuitableBuildPosition(const tagInfo& myInfo);
    std::pair<int, int> findBuildPosAroundBuilder(const tagInfo& myInfo, int builderSN);
    bool findEmpty2x2(const tagInfo& info, int startX, int startY, int& foundX, int& foundY);
    bool is2x2Empty(const tagInfo& info, int x, int y) const;
    bool isAreaSuitableForBuilding(int x, int y, const tagInfo& myInfo);
    
    // 谷仓建造相关函数
    void checkGranaryStatus(const tagInfo& myInfo);
    void checkFisherStatus(const tagInfo& myInfo);
    
    // 谷仓建造和时代升级相关函数
    void buildGranary(const tagInfo& myInfo);
    void upgradeToToolAge(const tagInfo& myInfo);
    
    // 村民生产相关函数
    void produceVillagers(const tagInfo& myInfo);
    
    // 农民工作分配相关函数
    void assignFarmerWork(const tagInfo& myInfo);
    void assignHunter(const tagInfo& myInfo);
    void assignWoodcutter1(const tagInfo& myInfo);
    void assignWoodcutter2(const tagInfo& myInfo);
    void assignAnimalHunter(const tagInfo& myInfo);
    void ensureHunterWorking(const tagInfo& myInfo);
    void ensureWoodcutterWorking(const tagInfo& myInfo, int woodcutterSN);
    void ensureAnimalHunterWorking(const tagInfo& myInfo);
    
    // 资源查找相关函数
    std::pair<int, int> findNearestResource(const tagInfo& myInfo, int resourceType);
    std::pair<int, int> findNearestAnimalResource(const tagInfo& myInfo);
    std::pair<int, int> findNearestFishResource(const tagInfo& myInfo);
    int findNearestFishResourceSN(const tagInfo& myInfo);
    std::pair<int, int> findNearestTreeToFarmer(const tagInfo& myInfo, int farmerSN);
    int findNearestTreeToFarmerSN(const tagInfo& myInfo, int farmerSN);
    std::pair<int, int> findNearestAnimalToFarmer(const tagInfo& myInfo, int farmerSN);
    int findNearestAnimalToFarmerSN(const tagInfo& myInfo, int farmerSN);
    
    // 军队处理相关函数
    void handleArmy(const tagInfo& myInfo);
    
    // 辅助函数
    bool isFarmerAlive(int farmerSN, const tagInfo& myInfo);
    bool canBuilderReachPosition(int builderSN, int targetX, int targetY, const tagInfo& myInfo);
    bool relocateBuilderOutOfFootprint(int builderSN, int buildX, int buildY, const tagInfo& myInfo);
    bool isSingleTileFreeFlatGrass(int x, int y, const tagInfo& myInfo);
    bool moveBuilderToExplore(int builderSN, const tagInfo& myInfo);
    // 搜索状态（环形扩张用）
    int m_lastCenterX = -1;
    int m_lastCenterY = -1;
    int m_searchRadius = 0;
    
    int AddToIns(instruction ins);
    void clearInsRet();
};

#endif // USRAI_H
